package com.ruoyi.slope.gateway.service.impl;

import java.io.IOException;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.slope.gateway.util.IOClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.slope.gateway.mapper.SlopeGatewayInfoMapper;
import com.ruoyi.slope.gateway.domain.SlopeGatewayInfo;
import com.ruoyi.slope.gateway.service.ISlopeGatewayInfoService;

/**
 * 边坡网关信息Service业务层处理
 * 
 * @author psy
 * @date 2025-05-18
 */
@Service
public class SlopeGatewayInfoServiceImpl implements ISlopeGatewayInfoService 
{
    @Autowired
    private SlopeGatewayInfoMapper slopeGatewayInfoMapper;

    public int alarm(String gatewaySn){
        try {
            int result = IOClient.alarm(gatewaySn);
            if(result == 1){
                SlopeGatewayInfo slopeGatewayInfo = new SlopeGatewayInfo();
                slopeGatewayInfo.setGatewaySn(gatewaySn);
                slopeGatewayInfo.setAlarmSwitch(1L);
                slopeGatewayInfoMapper.updateSlopeGatewaySwitch(slopeGatewayInfo);
            }
            return result;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public int cancelAlarm(String gatewaySn){
        try {
            int result = IOClient.cancelAlarm(gatewaySn);
            if(result == 1){
                SlopeGatewayInfo slopeGatewayInfo = new SlopeGatewayInfo();
                slopeGatewayInfo.setGatewaySn(gatewaySn);
                slopeGatewayInfo.setAlarmSwitch(0L);
                slopeGatewayInfoMapper.updateSlopeGatewaySwitch(slopeGatewayInfo);
            }
            return result;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public int getData(String gatewaySn){
        try {
            return IOClient.getData(gatewaySn);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public int getDataTime(String gatewaySn, String time){
        try {
            return IOClient.getDataTime(gatewaySn,time);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 查询边坡网关信息
     * 
     * @param id 边坡网关信息主键
     * @return 边坡网关信息
     */
    @Override
    public SlopeGatewayInfo selectSlopeGatewayInfoById(Long id)
    {
        return slopeGatewayInfoMapper.selectSlopeGatewayInfoById(id);
    }

    /**
     * 查询边坡网关信息列表
     * 
     * @param slopeGatewayInfo 边坡网关信息
     * @return 边坡网关信息
     */
    @Override
    public List<SlopeGatewayInfo> selectSlopeGatewayInfoList(SlopeGatewayInfo slopeGatewayInfo)
    {
        return slopeGatewayInfoMapper.selectSlopeGatewayInfoList(slopeGatewayInfo);
    }

    /**
     * 新增边坡网关信息
     * 
     * @param slopeGatewayInfo 边坡网关信息
     * @return 结果
     */
    @Override
    public int insertSlopeGatewayInfo(SlopeGatewayInfo slopeGatewayInfo)
    {
        return slopeGatewayInfoMapper.insertSlopeGatewayInfo(slopeGatewayInfo);
    }

    /**
     * 修改边坡网关信息
     * 
     * @param slopeGatewayInfo 边坡网关信息
     * @return 结果
     */
    @Override
    public int updateSlopeGatewayInfo(SlopeGatewayInfo slopeGatewayInfo)
    {
        slopeGatewayInfo.setUpdateTime(DateUtils.getNowDate());
        return slopeGatewayInfoMapper.updateSlopeGatewayInfo(slopeGatewayInfo);
    }

    /**
     * 批量删除边坡网关信息
     * 
     * @param ids 需要删除的边坡网关信息主键
     * @return 结果
     */
    @Override
    public int deleteSlopeGatewayInfoByIds(Long[] ids)
    {
        return slopeGatewayInfoMapper.deleteSlopeGatewayInfoByIds(ids);
    }

    /**
     * 删除边坡网关信息信息
     * 
     * @param id 边坡网关信息主键
     * @return 结果
     */
    @Override
    public int deleteSlopeGatewayInfoById(Long id)
    {
        return slopeGatewayInfoMapper.deleteSlopeGatewayInfoById(id);
    }
}
